import fcntl

f = open("demo.txt", "wb")
fcntl.lockf(f, fcntl.LOCK_EX)
f.write("hello")
fcntl.lockf(f, fcntl.LOCK_UN)

fcntl.flock(f, fcntl.LOCK_WRITE)
f.write(" world")
fcntl.flock(f, fcntl.LOCK_UN)
f.close()
